/*
+--------------------------------------------------------------------------
|   Mblog [#RELEASE_VERSION#]
|   ========================================
|   Copyright (c) 2014, 2015 mtons. All Rights Reserved
|   http://www.mtons.com
|
+---------------------------------------------------------------------------
*/
package com.hefei.blog.base.utils;

import java.util.Arrays;
import java.util.Calendar;
import java.util.Date;
import java.util.Iterator;

import org.apache.commons.lang.RandomStringUtils;
import org.apache.commons.lang.time.DateFormatUtils;

import com.hefei.blog.base.upload.consts.FileConsts;

public class FileNameUtils {
	private static String DDHHMMSS = "ddHHmmss";
	
	private static String YYYYMMDDHHMMSS = "/yyyy/MMdd/ddHHmmss";
	
	/**
	 * 36个小写字母和数字
	 */
	public static final char[] N36_CHARS = { '0', '1', '2', '3', '4', '5', '6',
			'7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
			'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w',
			'x', 'y', 'z' };
	
//	/**
//	 * 生成当前年月日格式的文件路径
//	 * 
//	 * yyyyMM 200806
//	 * 
//	 * @return
//	 */
//	public static String genPathName() {
//		return DateFormatUtils.format(Calendar.getInstance().getTime(), YYYYMM);
//	}
	
	/**
	 * 生成文件名
	 * 以当前日、时间开头加4位随机数的文件名
	 * 
	 * ddHHmmss 03102230
	 * 
	 * @return 10位长度文件名
	 */
//	public static String genFileName() {
//		return DateFormatUtils.format(Calendar.getInstance().getTime(), DDHHMMSS) + RandomStringUtils.random(4, N36_CHARS);
//	}
//
//	/**
//	 * 生成文件名
//	 * 以当前时间开头加4位随机数的文件名
//	 * 
//	 * @param ext 文件名后缀，不带'.'
//	 * @return 10位长度文件名+文件后缀
//	 */
//	public static String genFileName(String ext) {
//		return genFileName() + "." + ext;
//	}
	
	public static String genPathAndFileName(String ext) {
		return DateFormatUtils.format(new Date(), YYYYMMDDHHMMSS) + RandomStringUtils.random(4, N36_CHARS) + "." + ext;
	}

	/**
	 * 生成文件名
	 * 以当前时间开头加4位随机数的文件名
	 * 
	 * @param ext 文件名后缀 .jpg \ jpg
	 * @return 10位长度文件名+文件后缀
	 */
	public static String randomFileName(String ext) {
		if(ext.indexOf(".") != -1){
			return randomFileName() + ext;
		}else{
			return randomFileName() + "." + ext;
		}
		
	}
	/**
	 * 生成文件名
	 * 以当前时间开头加4位随机数的文件名
	 * 
	 * @param ext 文件名后缀，不带'.'
	 * @return 10位长度文件名+文件后缀
	 */
	public static String randomFileName() {
		return DateFormatUtils.format(Calendar.getInstance().getTime(), DDHHMMSS) + RandomStringUtils.random(4, N36_CHARS);
	}
	
	/**
	 * @param filename  /2017/9/9/123sieo.jpg OR 123sieo.jpg
	 * @return jpg
	 */
	public static String getExt(String filename, boolean withDot) {
		int pos = filename.lastIndexOf(".");
		if(withDot){
			return filename.substring(pos);
		}
		return filename.substring(pos + 1);
	}
	/**
	 * @param filename  /2017/9/9/123sieo.jpg OR 123sieo.jpg
	 * @return 123sieo
	 */
	public static String getFilename(String filePathAndName) {
		int slashIndex = filePathAndName.lastIndexOf("/");
		int dotIndex = filePathAndName.lastIndexOf(".");
		if(slashIndex != -1){
			return filePathAndName.substring(slashIndex + 1, dotIndex);
		}else{
			return filePathAndName.substring(0,dotIndex);
		}
	}
	
	/**
	 * @param filename  /2017/9/9/123sieo.jpg OR 123sieo.jpg
	 * @return /2017/9/9/123sieo_width_height.jpg OR 123sieo_width_height.jpg
	 */
	public static String getFilename(String filePathAndName, String width, String height) {
		int dotIndex = filePathAndName.lastIndexOf(".");
		String path = filePathAndName.substring(0, dotIndex);
		return path + "_"+width + "_" + height + getExt(filePathAndName, true);
	}
	
	

	/**
	 * 文件类型判断
	 */
	public static boolean checkImageFileType(String fileName) {
		Iterator<String> type = Arrays.asList(FileConsts.allowFilesPic).iterator();
		while (type.hasNext()) {
			String ext = type.next();
			if (fileName.toLowerCase().endsWith(ext)) {
				return true;
			}
		}
		return false;
	}
	
	/**
	 * 文件类型判断
	 */
	public static boolean checkImageFileType(String fileName, String[] allowFilesPic) {
		Iterator<String> type = Arrays.asList(allowFilesPic).iterator();
		while (type.hasNext()) {
			String ext = type.next();
			if (fileName.toLowerCase().endsWith(ext)) {
				return true;
			}
		}
		return false;
	}
	
	public static void main(String[] args){
		System.out.println(getFilename("/2017/9/9/123sieo.jpg"));
		System.out.println(getExt("/2017/9/9/123sieo.jpg", true));
		System.out.println(getExt("/2017/9/9/123sieo.jpg", false));
		
		System.out.println(getFilename("123sieo.jpg"));
		System.out.println(getExt("123sieo.jpg", true));
		System.out.println(getExt("/2017/9/9/123sieo.jpg", false));
		
		System.out.println(getFilename("/123sieo.jpg"));
		System.out.println(getExt("/123sieo.jpg", true));
		System.out.println(getExt("/2017/9/9/123sieo.jpg", false));
		
		
		System.out.println(getFilename("/2017/9/9/123sieo.jpg", "100", "200"));
		System.out.println(getFilename("D:/2017/9/9/123sieo.jpg", "100", "200"));
		System.out.println(getFilename("/2017/9/9/123sieo.jpg", "100", "x"));
		System.out.println(getFilename("D:/2017/9/9/123sieo.jpg", "x", "200"));
	}
	
}
